package com.lzj.admin.controller;


import com.lzj.admin.exceptions.ParamsException;
import com.lzj.admin.model.RespBean;
import com.lzj.admin.pojo.User;
import com.lzj.admin.query.UserQuery;
import com.lzj.admin.service.IUserService;
import com.sun.org.apache.xpath.internal.operations.Bool;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.security.Principal;
import java.util.Map;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author 王亚栋
 * @since 2022-05-21
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

//    @RequestMapping("/login")
//    @ResponseBody
//    public RespBean login(String userName, String password, HttpSession session) {
//
//
//            User user = userService.login(userName, password);
//            session.setAttribute("user", user);
//            return RespBean.success("用户登录成功");
//    }

    /**
     * 点击基础资料进行跳转
     * @return
     */
    @RequestMapping("/setting")
    public String setting(HttpSession session){

        User user = (User) session.getAttribute("user");
        session.setAttribute("user",userService.getById(user.getId()));

        return "user/setting";
    }

    /**
     * 更新用户信息
     * @return
     */
    @RequestMapping("/updateUserInfo")
    @ResponseBody
    public RespBean updateUserInfo(User user){


            userService.updateUserInfo(user);

            return RespBean.success("用户信息修改成功！");


    }

    /**
     * 用户密码更新页
     * @return
     */
    @RequestMapping("/toPasswordPage")
    public String password(){

        return "user/password";
    }


    /**
     * 用户密码更新
     * @param principal
     * @param oldPassword
     * @param newPassword
     * @param confirmPassword
     * @return
     */
    @RequestMapping("/updateUserPassword")
    @ResponseBody
    public RespBean updateUserPassword(Principal principal,
                                       String oldPassword,
                                       String newPassword,
                                       String confirmPassword) {




        userService.updateUserPassword(principal.getName(), oldPassword, newPassword, confirmPassword);

        return RespBean.success("密码修改成功！");
    }

    @RequestMapping("/index")
    public String index(){

        return "user/user";
    }

    @RequestMapping("/list")
    @ResponseBody
    public Map<String,Object> userList(UserQuery userQuery){

        return userService.userList(userQuery);
    }

    @RequestMapping("/addOrUpdateUserPage")
    public String addOrUpdatePage(Integer id, Model model){

        if (null != id){
            model.addAttribute("user",userService.getById(id));
        }

        return "user/add_update";

    }

    /**
     * 用户添加
     * @param user
     * @return
     */
    @RequestMapping("/save")
    @ResponseBody
    public RespBean saveUser(User user){

         userService.saveUser(user);

        return RespBean.success("用户记录添加成功！");
    }

    @RequestMapping("/update")
    @ResponseBody
    public RespBean updateUser(User user){

        userService.updateUser(user);

        System.out.println("UserController updateUser:");

        return RespBean.success("用户记录更新成功！");
    }

    @RequestMapping("/delete")
    @ResponseBody
    public RespBean deleteUser(Integer[] ids){
        userService.deleteUser(ids);

        return RespBean.success("用户记录删除成功！");
    }

}
